package com.ogawa.a7517.fragment;

import android.content.res.Resources;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ogawa.a7517.Constant;
import com.ogawa.a7517.R;
import com.ogawa.a7517.bean.DeviceOffBean;
import com.ogawa.a7517.bean.DeviceStateBean;
import com.ogawa.a7517.bean.MassageArmchair;

public class AdjustFragment extends BaseFragment implements View.OnClickListener {
    private TextView tvReset, tvSpread, tvZero;
    private ImageView ivBackUp, ivBackDown, ivFootUp, ivFootDown, ivLegUp, ivLegDown;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_adjust;
    }

    @Override
    public void initView(View view) {
        tvReset = view.findViewById(R.id.tv_reset);
        tvReset.setOnClickListener(this);
        tvSpread = view.findViewById(R.id.tv_spread);
        tvSpread.setOnClickListener(this);
        tvZero = view.findViewById(R.id.tv_zero_gravity);
        tvZero.setOnClickListener(this);
        ivBackUp = view.findViewById(R.id.iv_back_up);
        ivBackUp.setOnClickListener(this);
        ivBackDown = view.findViewById(R.id.iv_back_down);
        ivBackDown.setOnClickListener(this);
        ivFootUp = view.findViewById(R.id.iv_foot_up);
        ivFootUp.setOnClickListener(this);
        ivFootDown = view.findViewById(R.id.iv_foot_down);
        ivFootDown.setOnClickListener(this);
        ivLegUp = view.findViewById(R.id.iv_leg_up);
        ivLegUp.setOnClickListener(this);
        ivLegDown = view.findViewById(R.id.iv_leg_down);
        ivLegDown.setOnClickListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        setTitle(getString(R.string.seat_adjust));
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_reset) {
            activity7517.publish(Constant.ZERO_GRAVITY, "0");
        } else if (id == R.id.tv_spread) {
            activity7517.publish(Constant.ZERO_GRAVITY, "4");
        } else if (id == R.id.tv_zero_gravity) {
            activity7517.publish(Constant.ZERO_GRAVITY, "3");
        } else if (id == R.id.iv_back_up) {
            activity7517.publish(Constant.BACK_LEG_POS_ADJUST, "0");
        } else if (id == R.id.iv_back_down) {
            activity7517.publish(Constant.BACK_LEG_POS_ADJUST, "1");
        } else if (id == R.id.iv_foot_up) {
            activity7517.publish(Constant.LEG_POS_ADJUST, "1");
        } else if (id == R.id.iv_foot_down) {
            activity7517.publish(Constant.LEG_POS_ADJUST, "0");
        } else if (id == R.id.iv_leg_up) {
            activity7517.publish(Constant.LEG_POS_ADJUST_X, "5");
        } else if (id == R.id.iv_leg_down) {
            activity7517.publish(Constant.LEG_POS_ADJUST_X, "4");
        }
    }

    @Override
    public void deviceStateChange() {
        super.deviceStateChange();
        DeviceStateBean deviceStatusBean = MassageArmchair.getInstance().getDeviceStatusBean();
        if (deviceStatusBean != null) {
            DeviceStateBean.FunctionsBean.ZeroGravityBean zeroGravity = deviceStatusBean.getFunctions().getZeroGravity();
            if (zeroGravity != null) {
                int gravity = Integer.parseInt(zeroGravity.getModelValue());
                setGravityState(gravity);
            }
            DeviceStateBean.FunctionsBean.BackLegPosAdjustBean backLegPosAdjust = deviceStatusBean.getFunctions().getBackLegPosAdjust();
            if (backLegPosAdjust != null) {
                int back = Integer.parseInt(backLegPosAdjust.getModelValue());
                setBackState(back);
            }
            DeviceStateBean.FunctionsBean.LegPosAdjustBean legPosAdjust = deviceStatusBean.getFunctions().getLegPosAdjust();
            if (legPosAdjust != null) {
                int leg = Integer.parseInt(legPosAdjust.getModelValue());
                setFootState(leg);
            }
            DeviceStateBean.FunctionsBean.LegPosAdjustXBean legPosAdjustX = deviceStatusBean.getFunctions().getLegPosAdjustX();
            if (legPosAdjustX != null) {
                int legX = Integer.parseInt(legPosAdjustX.getModelValue());
                setLegState(legX);
            }
        } else {
            DeviceOffBean deviceOffBean = MassageArmchair.getInstance().getDeviceOffBean();
            if (deviceOffBean != null) {
                DeviceOffBean.FunctionsBean.PowerOffZeroGravityBean OffGravity
                        = deviceOffBean.getFunctions().getPowerOffZeroGravity();
                if (OffGravity != null) {
                    int gravity = Integer.parseInt(OffGravity.getModelValue());
                    setGravityState(gravity);
                }
                DeviceOffBean.FunctionsBean.PowerOffBackPosBean powerOffBackPos
                        = deviceOffBean.getFunctions().getPowerOffBackPos();
                if (powerOffBackPos != null) {
                    int back = Integer.parseInt(powerOffBackPos.getModelValue());
                    setBackState(back==-1?-1:(back + 1) % 2);
                }
                DeviceOffBean.FunctionsBean.PowerOffLegPosYBean powerOffLegPosY
                        = deviceOffBean.getFunctions().getPowerOffLegPosY();
                if (powerOffLegPosY != null) {
                    int leg = Integer.parseInt(powerOffLegPosY.getModelValue());
                    setFootState(leg);
                }
                DeviceOffBean.FunctionsBean.PowerOffLegPosXBean powerOffLegPosX
                        = deviceOffBean.getFunctions().getPowerOffLegPosX();
                if (powerOffLegPosX != null) {
                    int legX = Integer.parseInt(powerOffLegPosX.getModelValue());
                    setLegState(legX);
                }
            }
        }
    }

    private void setLegState(int legX) {
        ivLegDown.setSelected(legX == 4);
        ivLegUp.setSelected(legX == 5);
    }

    private void setFootState(int leg) {
        ivFootDown.setSelected(leg == 0);
        ivFootUp.setSelected(leg == 1);
    }

    private void setBackState(int back) {
        ivBackUp.setSelected(back == 0);
//        ivFootDown.setSelected(back == 0);
        ivBackDown.setSelected(back == 1);
//        ivFootUp.setSelected(back == 1);
    }

    private void setGravityState(int gravity) {
        tvReset.setSelected(gravity == 0);
        tvSpread.setSelected(gravity == 4);
//                tvZero.setSelected(gravity == 1 || gravity == 2);
        switch (gravity) {
            case 1:
                setTextDrawableTop(getResources(), tvZero, R.drawable.btn_zerogravity1_s);
                break;
            case 2:
                setTextDrawableTop(getResources(), tvZero, R.drawable.btn_zerogravity2_s);
                break;
            default:
                setTextDrawableTop(getResources(), tvZero, R.drawable.btn_zerogravity_n);
                break;
        }
    }

    /**
     * 设置文字上方的图片资源
     *
     * @param resources
     * @param textView
     * @param drawableId 图片 ID
     */
    private void setTextDrawableTop(Resources resources, TextView textView, int drawableId) {
        textView.setCompoundDrawablesWithIntrinsicBounds(null, -1 == drawableId ? null : resources.getDrawable(drawableId), null, null);
    }
}
